<?php
namespace Admin\Controller;
use Think\Controller;

class TypeController extends Controller 
{   
    //显示添加模板
    public function index()
    {
        $this->display();
    }
    // 判断商品类别添加是否成功
    public function insert()
    {
        $type = D('type');
        $res = $type->index();

        switch ($res){
            case 'success1':
                $this->success('添加成功',U('admin/type/displayType'),2);
                break;
            case 'success2':
                $this->success('添加成功','',2);
                break;
            case 'edit':
                $this->success('修改成功',U('admin/type/displayType'),2);
                break;
            case 'non';
                $this->error('添加失败！类别名为空！','',2);
                break;
            case 'editerror':
                $this->error('修改失败！',U('admin/type/edit'),2);
                break;
            default:
                $this->error('添加失败！','',2);
        }
    }
    //将数据库的商品分类信息输出到视图模板
    public function displayType()
    {
        $type = D('type');
        $res = $type->outlook();
        $this->assign("res",$res);
        $this->display('index');
    }
    /**
     * [add_type 增加商品的子分类]
     */
    public function add_type()
    {
        
        $modpid = I('get.pid');
        $modpath = I('get.path');
        $modname = I('get.name');


        if( !empty($modpath) || !empty($modpid)){
            $pid =  I('get.id');
            $path = $modpath .I('get.id').",";
            $name = $modname;
        }else{
            $pid = 0;
            $path = '0,';
            $name = "根类别";
        }
        
        $this->assign("name",$name);
        $this->assign("pid",$pid);
        $this->assign("path",$path);

        $type = D('type');
        $data = I('post.');
        $res = $type->add($data);
        $this->display('add');
    }

    /**
     * [do_Del 删除子目录功能]
     * @return [type] [返回成功与否并提示]
     */
    public function do_Del()
    {
        
        $type = D('type');
        $res = $type->type_del();
        if($res=="non"){
            $this->error('有子目录，不可删除！','',2);
        }else{
            $this->success('删除成功','',2);
        }
    }
    /**
     * [edit 获取商品分类后进行数据的分配，便于进行展示修改]
     * @return [type] [description]
     */
    public function edit()
    {
        // $data = I('get.');
        $type = D("type");
        $res = $type->edit_type();
        $this->assign("name",$res['name']);
        $this->assign("id",$res['id']);
        $this->assign("path",$res['path']);
        $this->assign("pid",$res['pid']);
        $this->display('edit');
    }
   
}